/*
 * @(#)MtmDocumentRelationship.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.bean.MtmDocumentRelationship;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  MtmDocumentRelationshipDAObject.java

  This class is the data access bean for table "MTM_DOCUMENT_RELATIONSHIP".

  @author      Rollo Chan
  @company     DCIVision Limited
  @creation date   02/01/2004
  @version     $Revision: 1.21.4.3 $
*/
/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/08/18
 * BugID      : 6029
 * Description: Show the correct icon in when Audit Trail.
 * *********************************************************************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/08/25
 * BugID      : 6143
 * Description: Delete the shortCut with the document, 
 *              when you delete document in advance search
 * *********************************************************************************************
 */
public class MtmDocumentRelationshipDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.21.4.3 $";

  public static final String TABLE_NAME = "MTM_DOCUMENT_RELATIONSHIP";

  public MtmDocumentRelationshipDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("DOCUMENT_ID");
    this.vecDBColumn.add("RELATED_DOCUMENT_ID");
    this.vecDBColumn.add("RELATIONSHIP_TYPE");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
          tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
          tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          return(tmpMtmDocumentRelationship);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        */
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
          tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
          tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          tmpMtmDocumentRelationship.setRecordCount(totalNumOfRecord);
          tmpMtmDocumentRelationship.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmDocumentRelationship);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
          tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
          tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          result.add(tmpMtmDocumentRelationship);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmDocumentRelationship tmpMtmDocumentRelationship = (MtmDocumentRelationship)((MtmDocumentRelationship)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_DOCUMENT_RELATIONSHIP(ID, DOCUMENT_ID, RELATED_DOCUMENT_ID, RELATIONSHIP_TYPE, DESCRIPTION, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmDocumentRelationship.getDocumentID());
        setPrepareStatement(preStat, 3, tmpMtmDocumentRelationship.getRelatedDocumentID());
        setPrepareStatement(preStat, 4, tmpMtmDocumentRelationship.getRelationshipType());
        setPrepareStatement(preStat, 5, tmpMtmDocumentRelationship.getDescription());
        setPrepareStatement(preStat, 6, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 7, new Integer(0));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        preStat.executeUpdate();
        tmpMtmDocumentRelationship.setID(nextID);
        tmpMtmDocumentRelationship.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmDocumentRelationship.setCreateDate(currTime);
        tmpMtmDocumentRelationship.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmDocumentRelationship.setUpdateDate(currTime);
        tmpMtmDocumentRelationship.setUpdateCount(new Integer(0));
        tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
        tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
        return(tmpMtmDocumentRelationship);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmDocumentRelationship tmpMtmDocumentRelationship = (MtmDocumentRelationship)((MtmDocumentRelationship)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_DOCUMENT_RELATIONSHIP ");
        sqlStat.append("SET  DOCUMENT_ID=?, RELATED_DOCUMENT_ID=?, RELATIONSHIP_TYPE=?, DESCRIPTION=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=?, RECORD_STATUS=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmDocumentRelationship.getDocumentID());
        setPrepareStatement(preStat, 2, tmpMtmDocumentRelationship.getRelatedDocumentID());
        setPrepareStatement(preStat, 3, tmpMtmDocumentRelationship.getRelationshipType());
        setPrepareStatement(preStat, 4, tmpMtmDocumentRelationship.getDescription());
        setPrepareStatement(preStat, 5, new Integer(tmpMtmDocumentRelationship.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8, tmpMtmDocumentRelationship.getRecordStatus());
        setPrepareStatement(preStat, 9, tmpMtmDocumentRelationship.getID());
        setPrepareStatement(preStat, 10, tmpMtmDocumentRelationship.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmDocumentRelationship.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmDocumentRelationship.setUpdateDate(currTime);
          tmpMtmDocumentRelationship.setUpdateCount(new Integer(tmpMtmDocumentRelationship.getUpdateCount().intValue() + 1));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          return(tmpMtmDocumentRelationship);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmDocumentRelationship tmpMtmDocumentRelationship = (MtmDocumentRelationship)((MtmDocumentRelationship)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmDocumentRelationship.getID());
        setPrepareStatement(preStat, 2, tmpMtmDocumentRelationship.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmDocumentRelationship);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmDocumentRelationship tmpMtmDocumentRelationship = (MtmDocumentRelationship)this.oldValue;

    if (tmpMtmDocumentRelationship != null) {
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getDocumentID()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getRelatedDocumentID()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getRelationshipType()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getDescription()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getUpdateDate()));
    }

    tmpMtmDocumentRelationship = (MtmDocumentRelationship)obj;
    if (tmpMtmDocumentRelationship != null) {
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getID()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getDocumentID()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getRelatedDocumentID()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getRelationshipType()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getDescription()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmDocumentRelationship.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  /**
   * Gets the original Document ID by the related document ID
   * 
   * @param relatedDocID the related document
   * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
   * @return the original document ID
   * @throws ApplicationException
   */

  public synchronized Integer getTargetDocIDByRelatedDocID(Integer relatedDocID, String relationship) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer targetDocID = null;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.DOCUMENT_ID ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.RELATED_DOCUMENT_ID = ? ");
        if (relationship!=null){
          sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, relatedDocID);
        if (relationship!=null){
          this.setPrepareStatement(preStat, 3, relationship);
        }

        rs = preStat.executeQuery();
        if (rs.next()) {
          targetDocID = getResultSetInteger(rs, "DOCUMENT_ID");
          return(targetDocID);
        } else {
          return null;
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the MtmDocumentRelationship object by the related document ID and the relationship type
   * 
   * @param relatedDocID the related document ID
   * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
   * @return abstract object of MtmDocumentRelationship object
   * @throws ApplicationException
   */
  public synchronized AbstractBaseObject getObjectByRelatedDocID(Integer relatedDocID, String relationship) throws ApplicationException {
      PreparedStatement preStat = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      MtmDocumentRelationship targetDocID = null;

      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
          sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.RELATED_DOCUMENT_ID = ? ");
          if (relationship!=null){
            sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
          }
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
          this.setPrepareStatement(preStat, 2, relatedDocID);
          if (relationship!=null){
            this.setPrepareStatement(preStat, 3, relationship);
          }

          rs = preStat.executeQuery();
          if (rs.next()) {
            MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
            tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
            tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
            tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
            tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
            tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
            tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
            tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
            return(tmpMtmDocumentRelationship);

          } else {
            return (null);
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }
    
  /**
   * Gets all the related document by the original document ID
   * 
   * @param targetDocID the original document ID
   * @return list of the MtmDocumentRelationship objects which are the related doc. of the specified targetDoc ID
   * @throws ApplicationException
   */
    public synchronized List getAllRelatedDocumentListByTargetDocID(Integer targetDocID) throws ApplicationException {
      PreparedStatement preStat = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List result = new ArrayList();

      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
          sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.DOCUMENT_ID=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
          this.setPrepareStatement(preStat, 2, targetDocID);
          rs = preStat.executeQuery();
          while (rs.next()) {
            MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
            tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
            tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
            tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
            tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
            tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
            tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
            tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
            result.add(tmpMtmDocumentRelationship);
          }
          return(result);
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }
    
    /**
     * Gets list of the MtmDocumentRelationship objects by document ID and relation type
     * 
     * @param documentID the document ID, the original document
     * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
     * @return List of the MtmDocumentRelationship objects 
     * @throws ApplicationException
     */
    public synchronized List getListByIDRelationType(Integer documentID, String relationType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();


    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        sqlStat.append("AND A.DOCUMENT_ID = ? ");

        if (!Utility.isEmpty(relationType)){
          sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, documentID);

        if(!Utility.isEmpty(relationType)){
          this.setPrepareStatement(preStat, 3, relationType);
        }
          rs = preStat.executeQuery();
        while (rs.next()) {
          MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
          tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
          tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          result.add(tmpMtmDocumentRelationship);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the list of the MtmDocumentRelationship objects by related document ID and relation type
   * 
   * @param relatedDocID the related document ID
   * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
   * @return List of the MtmDocumentRelationship objects
   * @throws ApplicationException
   */
  public synchronized List getListByRelationIDRelationType(Integer relatedDocID, String relationType) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   List result = new ArrayList();


   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
       sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
       sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
       sqlStat.append("AND A.RELATED_DOCUMENT_ID = ? ");

       if (!Utility.isEmpty(relationType)){
         sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
       }
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
       this.setPrepareStatement(preStat, 2, relatedDocID);

       if(!Utility.isEmpty(relationType)){
         this.setPrepareStatement(preStat, 3 , relationType);
       }
         rs = preStat.executeQuery();
       while (rs.next()) {
         MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
         tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
         tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
         tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
         tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
         tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
         tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
         tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
         result.add(tmpMtmDocumentRelationship);
       }
       return(result);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }



  /**
   * Delete the record of the table 'MTM_DOCUMENT_RELATIONSHIP' by specified ID
   * 
   * @param ID the ID of the record, i.e. same as the MtmDocumentRelationship object ID
   * @throws ApplicationException
   */
  public synchronized void deleteRelationshipByID(Integer ID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();


    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP ");
        sqlStat.append("WHERE  ID = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, ID);


        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Delete all the record of the document that is the original document or related document by the document ID provided
   * 
   * @param dmsDocumentID the dmsDocument object ID
   * @throws ApplicationException
   */
  public synchronized void deleteDirectIndirectRelationshipByID(Integer dmsDocumentID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
    try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP ");
        sqlStat.append("WHERE   DOCUMENT_ID = ? OR RELATED_DOCUMENT_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, dmsDocumentID);
        setPrepareStatement(preStat, 2,dmsDocumentID);
        updateCnt = preStat.executeUpdate();
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
     } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
    }
  }




  /**
   * Checks the exists of document relation by document ID, related document ID and the relationship type
   * 
   * @param documentID the original document ID
   * @param relatedDocID the related document InD
   * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
   * @return true if record exists, otherwise return false
   * @throws ApplicationException
   */
  public synchronized boolean checkExistRelation(Integer documentID, Integer relatedDocID, String relationshipType) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   Integer targetDocID = null;

   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT 1 ");
       sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
       sqlStat.append("WHERE  A.DOCUMENT_ID = ? ");
       sqlStat.append("AND A.RELATED_DOCUMENT_ID = ? ");
       sqlStat.append("AND RECORD_STATUS = ? ");
       if (!Utility.isEmpty(relationshipType)){
         sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
       }
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       this.setPrepareStatement(preStat, 1, documentID);
       this.setPrepareStatement(preStat,2,relatedDocID);
       this.setPrepareStatement(preStat,3,GlobalConstant.RECORD_STATUS_ACTIVE);
       if (!Utility.isEmpty(relationshipType)){
         this.setPrepareStatement(preStat, 4, relationshipType);
       }

       rs = preStat.executeQuery();
       if (rs.next()) {
         return(true);
       } else {
         return(false);
       }

     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 /**
  * Gets the list of the related document ID by original document ID, relation type and record status
  * 
  * @param documentID the original document ID
  * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
  * @param recordStatus the record status
  * @return list of the related document object ID
  * @throws ApplicationException
  */
 public synchronized List getRelatedDocIDByDocID(Integer documentID, String relationship, String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer targetDocID = null;
    List relatedDocList = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.RELATED_DOCUMENT_ID ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.DOCUMENT_ID = ? ");
        if (relationship!=null){
          sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, recordStatus);
        this.setPrepareStatement(preStat, 2, documentID);
        if (relationship!=null){
          this.setPrepareStatement(preStat, 3, relationship);
        }

        rs = preStat.executeQuery();
        while (rs.next()) {
          targetDocID = getResultSetInteger(rs, "RELATED_DOCUMENT_ID");
          relatedDocList.add(targetDocID);
        
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
        return relatedDocList;
      
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
 
 /**
  * Gets the list of MtmDocumentRelationship objects by related document ID, relation type and record status
  * i.e. it is used in the recycle bin, to retrieve list of the inactive MtmDocumentRelationship objects 
  * 
  * @param relatedDocID the related document ID
  * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
  * @param recordStatus the record status
  * @return list of MtmDocumentRelationship objects
  * @throws ApplicationException
  */
 public synchronized List getInactiveListByRelationID(Integer relatedDocID, String relationType, String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        sqlStat.append("AND A.RELATED_DOCUMENT_ID = ? ");

        if (!Utility.isEmpty(relationType)){
          sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, recordStatus);
        this.setPrepareStatement(preStat, 2, relatedDocID);

        if(!Utility.isEmpty(relationType)){
          this.setPrepareStatement(preStat, 3 , relationType);
        }
          rs = preStat.executeQuery();
        while (rs.next()) {
          MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
          tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
          /* Jicle.Wang 6143 2006/08/25 Start */
//          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
//          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
          /* Jicle.Wang 6143 2006/08/25 End   */
          tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
          tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          result.add(tmpMtmDocumentRelationship);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
 
 /**
  * Gets list of the MtmDocumentRelationship objects by document ID, relation type and record status
  * 
  * @param docID the original document id
  * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
  * @param recordStatus the record status
  * @return the list of the MtmDocumentRelationship objects
  * @throws ApplicationException
  */
 
 public synchronized List getInactiveListByID(Integer docID, String relationType, String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        sqlStat.append("AND A.DOCUMENT_ID = ? ");

        if (!Utility.isEmpty(relationType)){
          sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, recordStatus);
        this.setPrepareStatement(preStat, 2, docID);

        if(!Utility.isEmpty(relationType)){
          this.setPrepareStatement(preStat, 3 , relationType);
        }
          rs = preStat.executeQuery();
        while (rs.next()) {
          MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
          tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
          /* Jicle.Wang 6143 2006/08/25 Start */
//          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
//          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
          /* Jicle.Wang 6143 2006/08/25 End   */
          tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
          tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
          tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
          result.add(tmpMtmDocumentRelationship);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
 
 /**
  * Update the corresponding MtmDocumentRelationship object
  * @param obj the abstract object of MtmDocumentRelationship
  * @throws ApplicationException
  */
 public void inactiveListUpdate(AbstractBaseObject obj) throws ApplicationException{
   try{
     this.update(obj);
   }catch(ApplicationException appEx){
     throw appEx;
   }
 }
 
 public synchronized AbstractBaseObject insertOnly(AbstractBaseObject obj) throws ApplicationException {
   return this.insert(obj);
 }
 
 public synchronized List getListByDocumentID(Integer documentId) throws ApplicationException {
   return this.getListByDocumentID(documentId, null, null);
 }
 
 public synchronized List getListByDocumentID(
     Integer documentId,
     String relationshipType,
     String recordStatus) throws ApplicationException {
   List lstId = new ArrayList();
   lstId.add(documentId);
   return this.getListByDocumentIDList(lstId, relationshipType, recordStatus);
 }
 
 public synchronized List getListByDocumentIDList(List documentIds) throws ApplicationException {
   return this.getListByDocumentIDList(documentIds, null, null);
 }
 
 public synchronized List getListByDocumentIDList(
     List documentIds, 
     String relationshipType,
     String recordStatus) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
       sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
       sqlStat.append("WHERE  A.DOCUMENT_ID IN (");
       for (int i=0; i<documentIds.size(); i++) {
         sqlStat.append(i!=0?",?":"?");
       }
       sqlStat.append(") ");
       if (relationshipType!=null) {
         sqlStat.append(" AND A.RELATIONSHIP_TYPE=? ");
       }
       if (recordStatus!=null) {
         sqlStat.append(" AND A.RECORD_STATUS=? ");
       }
       sqlStat.append("ORDER BY A.DOCUMENT_ID");
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       int counter = 1;
       for (int i=0; i<documentIds.size(); i++) {
         this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
       }
       if (relationshipType!=null) {
         this.setPrepareStatement(preStat, counter++, relationshipType);
       }
       if (recordStatus!=null) {
         this.setPrepareStatement(preStat, counter++, recordStatus);
       }
       rs = preStat.executeQuery();

       List result = new ArrayList();
       while (rs.next()) {
         MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
         tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
         tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
         tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
         tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
         tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
         tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
         tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
         result.add(tmpMtmDocumentRelationship);
       }
       return(result);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 public synchronized List getListByRelatedDocumentID(
     Integer documentId, 
     String relationshipType,
     boolean matchType,
     String recordStatus,
     boolean matchStatus) throws ApplicationException {
   List lstId = new ArrayList();
   lstId.add(documentId);
   return this.getListByRelatedDocumentIDList(lstId, relationshipType, matchType, recordStatus, matchStatus);
 }
 
 public synchronized List getListByRelatedDocumentIDList(List documentIds) throws ApplicationException {
   return this.getListByRelatedDocumentIDList(documentIds, null, null);
 }
 
 public synchronized List getListByRelatedDocumentIDList(
     List documentIds, 
     String relationshipType,
     String recordStatus) throws ApplicationException {
   return this.getListByRelatedDocumentIDList(documentIds, relationshipType, true, recordStatus, true);
 }
 
 public synchronized List getListByRelatedDocumentIDList(
     List documentIds, 
     String relationshipType,
     boolean matchType,
     String recordStatus,
     boolean matchStatus) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.RELATED_DOCUMENT_ID, A.RELATIONSHIP_TYPE, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
       sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
       sqlStat.append("WHERE  A.RELATED_DOCUMENT_ID IN (");
       for (int i=0; i<documentIds.size(); i++) {
         sqlStat.append(i!=0?",?":"?");
       }
       sqlStat.append(") ");
       if (relationshipType!=null) {
         sqlStat.append(" AND A.RELATIONSHIP_TYPE"+(matchType ? "=": "<>")+"? ");
       }
       if (recordStatus!=null) {
         sqlStat.append(" AND A.RECORD_STATUS"+(matchStatus ? "=": "<>")+"? ");
       }
       sqlStat.append("ORDER BY A.DOCUMENT_ID");
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       int counter = 1;
       for (int i=0; i<documentIds.size(); i++) {
         this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
       }
       if (relationshipType!=null) {
         this.setPrepareStatement(preStat, counter++, relationshipType);
       }
       if (recordStatus!=null) {
         this.setPrepareStatement(preStat, counter++, recordStatus);
       }
       rs = preStat.executeQuery();

       List result = new ArrayList();
       while (rs.next()) {
         MtmDocumentRelationship tmpMtmDocumentRelationship = new MtmDocumentRelationship();
         tmpMtmDocumentRelationship.setID(getResultSetInteger(rs, "ID"));
         tmpMtmDocumentRelationship.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
         tmpMtmDocumentRelationship.setRelatedDocumentID(getResultSetInteger(rs, "RELATED_DOCUMENT_ID"));
         tmpMtmDocumentRelationship.setRelationshipType(getResultSetString(rs, "RELATIONSHIP_TYPE"));
         tmpMtmDocumentRelationship.setDescription(getResultSetString(rs, "DESCRIPTION"));
         tmpMtmDocumentRelationship.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpMtmDocumentRelationship.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpMtmDocumentRelationship.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpMtmDocumentRelationship.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpMtmDocumentRelationship.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpMtmDocumentRelationship.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpMtmDocumentRelationship.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getCreatorID()));
         tmpMtmDocumentRelationship.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmDocumentRelationship.getUpdaterID()));
         result.add(tmpMtmDocumentRelationship);
       }
       return(result);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 public synchronized void updateStatusByDocumentIDList(List documentIds, String recordStatus) throws ApplicationException {
   PreparedStatement preStat = null;
   StringBuffer sqlStat = new StringBuffer();
   synchronized(dbConn) {
     try {
       sqlStat.append("UPDATE MTM_DOCUMENT_RELATIONSHIP ");
       sqlStat.append("SET RECORD_STATUS=? ");
       sqlStat.append("WHERE DOCUMENT_ID IN (");
       for (int i=0; i<documentIds.size(); i++) {
         sqlStat.append(i!=0?",?":"?");
       }
       sqlStat.append(") OR RELATED_DOCUMENT_ID IN(");
       for (int i=0; i<documentIds.size(); i++) {
         sqlStat.append(i!=0?",?":"?");
       }
       sqlStat.append(")");
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       int counter = 1;
       this.setPrepareStatement(preStat, counter++, recordStatus);
       for (int i=0; i<documentIds.size(); i++) {
         this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
       }
       for (int i=0; i<documentIds.size(); i++) {
         this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
       }
       preStat.executeUpdate();
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
     } finally {
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }

 /* Jicle.Wang 6029 2006/08/18 Start */
 /**
  * Gets the list of the document ID by original related document ID, relation type and record status
  * 
  * @param documentID the original document ID
  * @param relationship the type of relationship specified in option setup, but it is 'L' for shortcut relation
  * @param recordStatus the record status
  * @return list of the related document object ID
  * @throws ApplicationException
  */
 public synchronized List getDocIDByRelatedDocID(Integer documentID, String relationship, String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer targetDocID = null;
    List relatedDocList = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.DOCUMENT_ID ");
        sqlStat.append("FROM   MTM_DOCUMENT_RELATIONSHIP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.RELATED_DOCUMENT_ID = ? ");
        if (relationship!=null){
          sqlStat.append("AND A.RELATIONSHIP_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, recordStatus);
        this.setPrepareStatement(preStat, 2, documentID);
        if (relationship!=null){
          this.setPrepareStatement(preStat, 3, relationship);
        }

        rs = preStat.executeQuery();
        while (rs.next()) {
          targetDocID = getResultSetInteger(rs, "DOCUMENT_ID");
          relatedDocList.add(targetDocID);
        
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
        return relatedDocList;
      
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
 
 /**
  * bulk update MTM_DOCUMENT_RELATIONSHIP RECORD_STATUS according to id
  * @param idList list table of Integer ids
  * @return true or false 
  * @throws ApplicationException
  */
 public synchronized boolean updateListRecordStatus(List idList) throws ApplicationException{
   boolean returnValue = false;
   if(Utility.isEmpty(idList)) {
     return returnValue;
   }
   PreparedStatement pst = null;
   StringBuffer sql = new StringBuffer();
   sql.append("UPDATE MTM_DOCUMENT_RELATIONSHIP SET RECORD_STATUS= '")
   .append(GlobalConstant.RECORD_STATUS_ACTIVE)
   .append("' WHERE id in(");
   for(int i=0;i<idList.size();i++) {
     sql.append("?");
     if(i!=idList.size()-1) {
       sql.append(",");
     }
   }
   sql.append(")");
   synchronized(dbConn) {
     try {
       pst =  dbConn.prepareStatement(sql.toString());
       for(int i=1;i<=idList.size();i++) {
         pst.setInt(i, ((Integer)idList.get(i-1)).intValue());
       }
       returnValue = pst.execute();
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     }finally {
       try {pst.close();} catch(Exception ignore) {pst = null;}
     }
   }
   return returnValue;
 }
 /* Jicle.Wang 6029 2006/08/18 End   */
}
